﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HSIA
{
    public partial class FrmMonitor : Form
    {
        public delegate bool SendOrder(string sIP, byte[] byteHex);
        SendOrder netSend;

        public FrmMonitor(SendOrder sendOrder, DataOrder ao, DataGridView dgv)
        {
            netSend = sendOrder;
            dgvReciver = dgv;
            dataOrder = ao;
            InitializeComponent();
        }

        DataOrder dataOrder;
        readonly DataGridView dgvReciver;

        private void FrmMonitor_Load(object sender, EventArgs e)
        {
            tbMonitor.Clear();
            tbMonitor.AppendText("[" + PublicFunc.GetUniformDateString(DateTime.Now) + "]:Monitoring...\r\n");
            cbSend.DataSource = dataOrder.dtOrderType;
            cbSend.ValueMember = "Head";
        }

        private void tsmGatherSelected_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbMonitor.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs);
            }
        }

        private void tFrm_MouseEnter(object sender, EventArgs e)
        {
            ToolStripMenuItem tsm = sender as ToolStripMenuItem;
            if (tsm != null)
            {
                tsm.ForeColor = Color.Black;
            }
        }

        private void tFrm_MouseLeave(object sender, EventArgs e)
        {
            ToolStripMenuItem tsm = sender as ToolStripMenuItem;
            if (tsm != null)
            {
                tsm.ForeColor = Color.White;
            }
        }

        private void tsmExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 记录数据iType:0发送,1接收
        /// </summary>
        /// <param name="sTitle"></param>
        /// <param name="byteData"></param>
        /// <param name="iType"></param>
        public void AddData(string sTitle, byte[] byteData, int iType)
        {
            if (byteData == null || byteData.Length < 1) return;
            tbMonitor.SelectionColor = (iType == 0) ? Color.Blue : Color.Green;
            tbMonitor.AppendText(((iType == 0) ? "Send_To:" : "Receive_From:") + "[" + sTitle + "]:\r\n");
            string sHex = "";
            foreach (byte b in byteData) sHex += b.ToString("X2") + " ";
            tbMonitor.AppendText(sHex + "\r\n");
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmMonitor_Load(null, null);
        }

        private void btSend_Click(object sender, EventArgs e)
        {
            string sOrder = cbSend.Text;
            DataGridViewSelectedRowCollection slDR = dgvReciver.SelectedRows;
            foreach (DataGridViewRow dgvr in slDR)
            {
                DataRow drReceiver = (dgvr.DataBoundItem as DataRowView).Row;
                byte[] byteHex = (ckCRC.Checked) ? dataOrder.GetOrderReceiver(ref sOrder, 0,8) : dataOrder.GetOrderFromString(ref sOrder);
                netSend(drReceiver["Port"].ToString(), byteHex);
            }
        }

        private void cbSend_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataGridViewRow dgvr = dgvReciver.CurrentRow;
            DataRow drReceiver = (dgvr.DataBoundItem as DataRowView).Row;
            if (drReceiver["ReceiverId"] != DBNull.Value) cbSend.Text += Convert.ToInt32(drReceiver["ReceiverId"]).ToString("X4");
        }

    }
}
